package com.rc.evcharger.redis;

import com.rc.evcharger.common.dto.ChargeBalaceDto;
import com.rc.evcharger.common.redis.RedisDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

@Service
public class RedPileServiceImpl implements RedPileService {
    Logger logger = LoggerFactory.getLogger(RedPileServiceImpl.class);

    @Autowired
    private RedisDao redisDao;

    /*@Override
    public void putPileToRedis(String rtua, List<CurrDataResult> list) {
        try {
            String gunNum = null;
            BigDecimal volt = null, elec = null;
            for (CurrDataResult cdr : list) {
                if (cdr.getCode().equals(DataUtil.dataCodes[7][0].toString())) {
                    volt = DataUtil.getBDValue(cdr.getValue(), DataUtil.getValueRate(cdr.getCode()), false);
                } else if (cdr.getCode().equals(DataUtil.dataCodes[8][0].toString())) {
                    elec = DataUtil.getBDValue(cdr.getValue(), DataUtil.getValueRate(cdr.getCode()), false);
                } else if (cdr.getCode().equals("1F B6")) {
                    String str = cdr.getValue().substring(0, (int) DataUtil.dataCodes[9][1]);
                    volt = DataUtil.getBDValue(str, (int) DataUtil.dataCodes[9][3], false);
                } else if (cdr.getCode().equals("2F B6")) {
                    String str = cdr.getValue().substring(0, (int) DataUtil.dataCodes[10][1]);
                    elec = DataUtil.getBDValue(str, (int) DataUtil.dataCodes[10][3], false);
                }
                gunNum = cdr.getGunNum();
            }
            if (gunNum == null || volt == null || elec == null) {
                return;
            }
            Pile pile = new Pile(rtua, gunNum, volt, elec);
            logger.warn("读到的实时数据：" + pile.toString());
            redisDao.leftPushList(Pile.PILE, pile.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    @Override
    public void putPileToRedis(String rtua, ChargeBalaceDto cbdto) {
        String gunNum = cbdto.getGunNum();
        BigDecimal power = cbdto.getPower();
        BigDecimal volt = cbdto.getVoltage();
        BigDecimal elec = cbdto.getElec();

        if (gunNum == null || volt == null || elec == null) {
            return;
        }

        RedPile pile = new RedPile(rtua, gunNum, volt, elec, power);
        logger.warn("读到的实时数据：" + pile.toString());
        redisDao.putHashSession(RedPile.PILE, pile.getKey(), pile.toString());
    }

    @Override
    public void delHashKeyMap(String rtua, String gunNum) {
        if (gunNum.length() < 2) {
            gunNum = "0" + gunNum;
        }
        String pileKey = rtua + ":" + gunNum;
        redisDao.delHashKeyMap(RedPile.PILE, pileKey);
    }


    @Override
    public boolean checkExpireGun(String rtua, String gun, Integer milliSecond) {
        String key = "EXPIRE:" + rtua + ":" + gun;
        Object redis = redisDao.getOpsForRedis(key);
        if (redis == null) {
            redisDao.putRedis(key,"1");
            redisDao.addExpireValue(key, 1, milliSecond, TimeUnit.MILLISECONDS);
            return false;
        }
        return true;
    }


    @Override
    public boolean checkHlhtExpireReq(String orderNum, Integer milliSecond) {
        String key = "EXPIRE:HLHT:REQ:" + orderNum;
        Object redis = redisDao.getOpsForRedis(key);
        if (redis == null) {
            //redisDao.putRedis(key, "1");
            redisDao.addExpireValue(key, 1, milliSecond, TimeUnit.MILLISECONDS);
            return false;
        }
        return true;
    }
}
